package nets

import (
	"github.com/kataras/golog"
	"net"
	"strings"
)

func GetLocalIfAddrs() []string {
	addrs := []string{}
	interfaces, err := net.Interfaces()
	if err != nil {
		golog.Errorf("call net.Interfaces failed. err: %+v", err)
		return addrs
	}

	for _, i := range interfaces {
		addresses, err := i.Addrs()
		if err != nil {
			golog.Error("get interface address failed. name: %s, err: %+v", i.Name, err)
			continue
		}

		for _, v := range addresses {
			if ipNet, ok := v.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
				if ipNet.IP.To4() != nil {
					seps := strings.Split(v.String(), "/")
					addrs = append(addrs, seps[0])
				}
			}
		}
	}
	return addrs
}
